home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / speed.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  9KB  |  443 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #define _HPUX_SOURCE
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include "gltk.h"
  32.  
  33. #ifdef __unix
  34. #include <sys/times.h>
  35. #include <sys/param.h>
  36. #endif
  37. #ifdef __bsdi
  38. #include <time.h>
  39. #endif
  40.  
  41. #define GAP 10
  42. #define ROWS 1
  43. #define COLS 4
  44.  
  45. GLenum rgb, doubleBuffer, directRender, windType;
  46. GLint windW, windH;
  47.  
  48. GLint boxW, boxH;
  49.  
  50. GLenum antialiasing = GL_FALSE;
  51. GLenum depthTesting = GL_FALSE;
  52. GLenum fogging = GL_FALSE, niceFogging = GL_FALSE;
  53. GLenum lighting = GL_FALSE;
  54. GLenum shading = GL_FALSE;
  55. GLenum texturing = GL_FALSE;
  56.  
  57. GLint repeatCount = 1000;
  58. GLint loopCount = 100;
  59.  
  60. GLubyte texture[4 * 3] =
  61. {
  62.   0xFF, 0, 0, 0, 0, 0,
  63.   0, 0, 0, 0, 0xFF, 0,
  64. };
  65.  
  66. static void SetWindSize(int width, int height)
  67. {
  68.  
  69.   windW = (GLint) width;
  70.   windH = (GLint) height;
  71. }
  72.  
  73. static GLenum Key(int key, GLenum mask)
  74. {
  75.  
  76.   switch (key) {
  77.     case TK_ESCAPE:
  78.       tkQuit();
  79.     case TK_a:
  80.       antialiasing = !antialiasing;
  81.       break;
  82.     case TK_d:
  83.       depthTesting = !depthTesting;
  84.       break;
  85.     case TK_f:
  86.       fogging = !fogging;
  87.       break;
  88.     case TK_F:
  89.       niceFogging = !niceFogging;
  90.       break;
  91.     case TK_s:
  92.       shading = !shading;
  93.       break;
  94.     case TK_t:
  95.       texturing = !texturing;
  96.       break;
  97.     default:
  98.       return GL_FALSE;
  99.   }
  100.   return GL_TRUE;
  101. }
  102.  
  103. static void Viewport(GLint row, GLint column)
  104. {
  105.   GLint x, y;
  106.  
  107.   boxW = (windW - (COLS + 1) * GAP) / COLS;
  108.   boxH = (windH - (ROWS + 1) * GAP) / ROWS;
  109.  
  110.   x = GAP + column * (boxW + GAP);
  111.   y = GAP + row * (boxH + GAP);
  112.  
  113.   glViewport(x, y, boxW, boxH);
  114.  
  115.   glMatrixMode(GL_PROJECTION);
  116.   glLoadIdentity();
  117.   gluOrtho2D(-boxW / 2, boxW / 2, -boxH / 2, boxH / 2);
  118.   glMatrixMode(GL_MODELVIEW);
  119.  
  120.   /*
  121.    * glEnable(GL_SCISSOR_TEST);
  122.    */
  123.   glScissor(x, y, boxW, boxH);
  124. }
  125.  
  126. static double Now(void)
  127. {
  128. #ifdef __unix
  129.   struct tms tm;
  130.  
  131. #ifdef __MACHTEN__
  132.  
  133.   times(&tm);
  134.  
  135.   return (double)tm.tms_utime / (double)CLK_TCK;
  136. #else
  137.   clock_t clk;
  138.  
  139.   clk = times(&tm);
  140.  
  141. #ifdef CLK_TCK
  142.   return (double)clk / (double)CLK_TCK;
  143. #else
  144.   return (double)clk / (double)HZ;
  145. #endif
  146. #endif
  147.   return 0;
  148. #endif
  149. }
  150.  
  151. static void Report(const char *msg, float elapsed)
  152. {
  153.  
  154.   if (elapsed == 0.0) {
  155.     printf("%s per second: Unknown, elapsed time is zero\n", msg);
  156.   }
  157.   else {
  158.     printf("%s per second: %g\n", msg, repeatCount * loopCount / elapsed);
  159.   }
  160. }
  161.  
  162. static void Points(void)
  163. {
  164.   GLint i, j;
  165.   float v1[3];
  166.   double start;
  167.  
  168.   start = Now();
  169.   for (i = 0; i < repeatCount; i++) {
  170.     v1[0] = 10;
  171.     v1[1] = 10;
  172.     v1[2] = 10;
  173.     glBegin(GL_POINTS);
  174.     for (j = 0; j < loopCount; j++) {
  175.       glVertex2fv(v1);
  176.     }
  177.     glEnd();
  178.   }
  179.   glFinish();
  180.   Report("Points", Now() - start);
  181. }
  182.  
  183. static void Lines(void)
  184. {
  185.   GLint i, j;
  186.   float v1[3], v2[3];
  187.   double start;
  188.  
  189.   start = Now();
  190.   for (i = 0; i < repeatCount; i++) {
  191.     v1[0] = 10;
  192.     v1[1] = 10;
  193.     v1[2] = 10;
  194.     v2[0] = 20;
  195.     v2[1] = 20;
  196.     v2[2] = 10;
  197.     glBegin(GL_LINES);
  198.     for (j = 0; j < loopCount; j++) {
  199.       glVertex2fv(v1);
  200.       glVertex2fv(v2);
  201.     }
  202.     glEnd();
  203.   }
  204.   glFinish();
  205.   Report("Lines", Now() - start);
  206. }
  207.  
  208. static void Triangles(void)
  209. {
  210.   GLint i, j;
  211.   float v1[3], v2[3], v3[3], t1[2], t2[2], t3[2];
  212.   double start;
  213.  
  214.   start = Now();
  215.  
  216.   v1[0] = 10;
  217.   v1[1] = 10;
  218.   v1[2] = 10;
  219.   v2[0] = 20;
  220.   v2[1] = 20;
  221.   v2[2] = 10;
  222.   v3[0] = 10;
  223.   v3[1] = 20;
  224.   v3[2] = 10;
  225.  
  226.   t1[0] = 0;
  227.   t1[1] = 0;
  228.   t2[0] = 1;
  229.   t2[1] = 1;
  230.   t3[0] = 0;
  231.   t3[1] = 1;
  232.  
  233.   for (i = 0; i < repeatCount; i++) {
  234.     glBegin(GL_TRIANGLES);
  235.     for (j = 0; j < loopCount; j++) {
  236.       if (texturing) {
  237.     glTexCoord2fv(t1);
  238.       }
  239.       glVertex2fv(v1);
  240.       if (texturing) {
  241.     glTexCoord2fv(t2);
  242.       }
  243.       glVertex2fv(v2);
  244.       if (texturing) {
  245.     glTexCoord2fv(t3);
  246.       }
  247.       glVertex2fv(v3);
  248.     }
  249.     glEnd();
  250.   }
  251.   glFinish();
  252.   Report("Triangles", Now() - start);
  253. }
  254.  
  255. static void Rects(void)
  256. {
  257.   GLint i, j;
  258.   float v1[2], v2[2];
  259.   double start;
  260.  
  261.   start = Now();
  262.   for (i = 0; i < repeatCount; i++) {
  263.     v1[0] = 10;
  264.     v1[1] = 10;
  265.     v2[0] = 20;
  266.     v2[1] = 20;
  267.     for (j = 0; j < loopCount; j++) {
  268.       glRectfv(v1, v2);
  269.     }
  270.   }
  271.   glFinish();
  272.   Report("Rects", Now() - start);
  273. }
  274.  
  275. static void Draw(void)
  276. {
  277.  
  278.   glClearColor(0.0, 0.0, 0.0, 0.0);
  279.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  280.  
  281.   TK_SETCOLOR(windType, TK_YELLOW);
  282.  
  283.   if (antialiasing) {
  284.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  285.     glEnable(GL_BLEND);
  286.  
  287.     glEnable(GL_POINT_SMOOTH);
  288.     glEnable(GL_LINE_SMOOTH);
  289.     glEnable(GL_POLYGON_SMOOTH);
  290.     printf("antialiasing: on\n");
  291.   }
  292.   else {
  293.     glDisable(GL_BLEND);
  294.     glDisable(GL_POINT_SMOOTH);
  295.     glDisable(GL_LINE_SMOOTH);
  296.     glDisable(GL_POLYGON_SMOOTH);
  297.     printf("antialiasing: off\n");
  298.   }
  299.   if (depthTesting) {
  300.     glEnable(GL_DEPTH_TEST);
  301.     printf("depthtest: on\n");
  302.   }
  303.   else {
  304.     glDisable(GL_DEPTH_TEST);
  305.     printf("depthtest: off\n");
  306.   }
  307.   if (fogging) {
  308.     glEnable(GL_FOG);
  309.     glHint(GL_FOG_HINT, (niceFogging) ? GL_NICEST : GL_FASTEST);
  310.     printf("fog: on\n");
  311.   }
  312.   else {
  313.     glDisable(GL_FOG);
  314.     printf("fog: off\n");
  315.   }
  316.   if (lighting) {
  317.     static GLfloat ambient[4] =
  318.     {1, 0.5, 0.5, 0};
  319.  
  320.     glEnable(GL_NORMALIZE);
  321.     glNormal3f(1.0, 1.0, 1.0);
  322.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  323.     glEnable(GL_LIGHTING);
  324.     glEnable(GL_LIGHT0);
  325.     printf("lighting: on\n");
  326.   }
  327.   else {
  328.     glDisable(GL_LIGHTING);
  329.     printf("lighting: off\n");
  330.   }
  331.   if (shading) {
  332.     glShadeModel(GL_SMOOTH);
  333.     printf("shading: smooth\n");
  334.   }
  335.   else {
  336.     glShadeModel(GL_FLAT);
  337.     printf("shading: flat\n");
  338.   }
  339.   if (texturing) {
  340.     static GLfloat modulate[1] =
  341.     {GL_DECAL};
  342.     static GLfloat clamp[1] =
  343.     {GL_CLAMP};
  344.     static GLfloat linear[1] =
  345.     {GL_LINEAR};
  346.  
  347.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  348.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE,
  349.          (GLvoid *) texture);
  350.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modulate);
  351.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
  352.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
  353.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear);
  354.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear);
  355.     glEnable(GL_TEXTURE_2D);
  356.     printf("texturing: on\n");
  357.   }
  358.   else {
  359.     glDisable(GL_TEXTURE_2D);
  360.     printf("texturing: off\n");
  361.   }
  362.  
  363.   Viewport(0, 0);
  364.   Points();
  365.   Viewport(0, 1);
  366.   Lines();
  367.   Viewport(0, 2);
  368.   Triangles();
  369.   Viewport(0, 3);
  370.   Rects();
  371.  
  372.   glFlush();
  373.  
  374.   if (doubleBuffer) {
  375.     tkSwapBuffers();
  376.   }
  377. }
  378.  
  379. static GLenum Args(int argc, char **argv)
  380. {
  381.   GLint i;
  382.  
  383.   rgb = GL_TRUE;
  384.   doubleBuffer = GL_FALSE;
  385.   directRender = GL_TRUE;
  386.  
  387.   for (i = 1; i < argc; i++) {
  388.     if (strcmp(argv[i], "-ci") == 0) {
  389.       rgb = GL_FALSE;
  390.     }
  391.     else if (strcmp(argv[i], "-rgb") == 0) {
  392.       rgb = GL_TRUE;
  393.     }
  394.     else if (strcmp(argv[i], "-sb") == 0) {
  395.       doubleBuffer = GL_FALSE;
  396.     }
  397.     else if (strcmp(argv[i], "-db") == 0) {
  398.       doubleBuffer = GL_TRUE;
  399.     }
  400.     else if (strcmp(argv[i], "-dr") == 0) {
  401.       directRender = GL_TRUE;
  402.     }
  403.     else if (strcmp(argv[i], "-ir") == 0) {
  404.       directRender = GL_FALSE;
  405.     }
  406.     else {
  407.       printf("%s (Bad option).\n", argv[i]);
  408.       return GL_FALSE;
  409.     }
  410.   }
  411.   return GL_TRUE;
  412. }
  413.  
  414. void main(int argc, char **argv)
  415. {
  416.  
  417.   if (Args(argc, argv) == GL_FALSE) {
  418.     tkQuit();
  419.   }
  420.  
  421.   windW = 600;
  422.   windH = 300;
  423.   tkInitPosition(0, 0, windW, windH);
  424.  
  425.   windType =                                       /*
  426.                                             * TK_ALPHA |
  427.                                             */ TK_DEPTH;
  428.   windType |= (rgb) ? TK_RGB : TK_INDEX;
  429.   windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  430.   windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  431.   tkInitDisplayMode(windType);
  432.  
  433.   if (tkInitWindow("Speed Test") == GL_FALSE) {
  434.     tkQuit();
  435.   }
  436.  
  437.   tkExposeFunc(SetWindSize);
  438.   tkReshapeFunc(SetWindSize);
  439.   tkKeyDownFunc(Key);
  440.   tkDisplayFunc(Draw);
  441.   tkExec();
  442. }
  443.